<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="danger" @click="loginOut">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="col? '64px':'200px'">
        <!-- 菜单折叠 -->
        <div class="collapse" @click="toggleMenu">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409BEF"
          unique-opened
          :collapse="col"
          :collapse-transition="false"
          router
          :default-active="curPath"
          @select="changepath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单内容 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 标题 -->
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/home/'+mitem.path+''"
              v-for="mitem in item.children"
              :key="mitem.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 标题 -->
                <span>{{mitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: "",
      iconObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao"
      },
      col: false,
      curPath: "/home/welcome"
    };
  },
  mounted () {
    // 获取左侧菜单内容
    this.axios.get("/menus").then(res => {
      const { data } = res;
      // 不为200 则请求失败
      if (res.meta.status !== 200) return this.$message.error(data.meta.msg);
      this.menuList = data;
      console.log(data);
    });
    // 给全局事件总线绑定一个事件  当点击子组件面包屑时 切换路
  },
  methods: {
    loginOut () {
      sessionStorage.removeItem("token");
      // 使用replace做退出  让用户无法返回
      this.$router.replace("/login");
    },
    toggleMenu () {
      this.col = !this.col;
    },
    // 选中对应的菜单项  将当前的path用索引的值表示
    changepath (index, indexPath) {
      this.curPath = index;
    }
  },
  // 路由监视
  watch: {
    $route: {
      handler (newRouter) {
        this.curPath = newRouter.path;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  color: #fff;
  text-align: center;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    img {
      margin-right: 10px;
    }
  }
}

.el-aside {
  transition: 1s;
  background-color: #333744;
  color: #333;
  overflow: hidden !important;
  // line-height: 200px;
  .collapse {
    background-color: #4a5064;
    text-align: center;
    color: #fff;
    line-height: 20px;
    letter-spacing: 0.2em;
    cursor: pointer;
    &:hover {
      background-color: #4aaaaa;
    }
  }
  .el-menu {
    border-right: 0;
    height: 100%;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}
.iconfont {
  margin-right: 25px;
}
</style>
